package com.dingding.queue.base.push;

import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.dingding.facade.queue.enums.RoleType;

import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.AndroidNotification.Builder;
import cn.jpush.api.push.model.notification.Notification;

/**
 * 
 * @ClassName: AndoridSendPush 
 * @Description: Andorid 消息推送实现类
 * @author CHEHAUNBO 
 * @date 2016年6月6日 下午11:04:11
 * 
 */
@Component("andoridSendPush")
@SuppressWarnings("all")
public class AndoridSendPush extends AbstractSendPush {

	private static Logger log = Logger.getLogger(AndoridSendPush.class);

	/**
	 * 
	 * 推送消息,包含(通知、标题、自定义内容、自定义参数)
	 * 
	 * @param aliasId
	 *            手机设备在极光绑定的设备别名
	 * @param alert
	 *            通知，显示在手机通知栏中的内容
	 * @param msgTitle
	 *            自定义标题
	 * @param msgContent
	 *            自定义消息的内容
	 * @param map 自定义参数的内容
	 * @param clientType
	 *            推送设备的类型(iOS/Android)
	 * 
	 * @author cheshaowei
	 * 
	 */
	@Override
	public boolean sendPush(String aliasId, String alert, String msgTitle, String msgContent, Map<String, String> map,
			RoleType roleType) {

		Builder builder = AndroidNotification.newBuilder();

		for (String key : map.keySet()) {
			builder.addExtra(key, map.get(key));
		}

		PushPayload pushPayload = PushPayload.newBuilder().setPlatform(Platform.android())
				.setAudience(Audience.alias(aliasId))
				.setNotification(
						Notification.newBuilder().addPlatformNotification(builder.setAlert(alert).build()).build())
				.setMessage(Message.newBuilder().setMsgContent(msgContent).build()).build();

		return excuteSendPush(pushPayload, roleType);

	}

	/**
	 * 
	 * 推送 只包含（通知、自定义参数）
	 * 
	 * @Title: sendPush 
	 * @param aliasId 推送别名
	 * @param alert  通知
	 * @param map 自定义参数
	 * @return  boolean true 成功 false 失败
	 * @author CHEHAUNBO
	 * @since V2.4.0
	 * 
	 */
	@Override
	public boolean sendPush(String aliasId, String alert, Map<String, String> map, RoleType roleType) {

		Builder builder = AndroidNotification.newBuilder();

		for (String key : map.keySet()) {
			builder.addExtra(key, map.get(key));
		}

		PushPayload pushPayload = PushPayload.newBuilder().setPlatform(Platform.android())
				.setAudience(Audience.alias(aliasId))
				.setNotification(
						Notification.newBuilder().addPlatformNotification(builder.setAlert(alert).build()).build())
				.build();

		return excuteSendPush(pushPayload, roleType);

	}

	/**
	 * 
	 * 推送消息，不包含(自定义标题)
	 * 
	 * @Description:封装iOS消息推送参数 
	 * @param aliasId 设备别名 
	 * @param alert 通知 
	 * @param msgContent 自定义消息内容 
	 * @author cheshaowei
	 *  
	 */
	@Override
	public boolean sendPush(String aliasId, String alert, String msgContent, RoleType roleType) {

		PushPayload pushPayload = PushPayload.newBuilder().setPlatform(Platform.android())
				.setAudience(Audience.alias(aliasId)).setNotification(Notification.alert(alert))
				.setMessage(Message.newBuilder().setMsgContent(msgContent).build()).build();

		return excuteSendPush(pushPayload, roleType);

	}

	/**
	 * 
	 * 推送消息，只包含(通知、自定义内容)
	 * @param aliasId 设备别名 
	 * @param msgContent 消息内容 
	 * @author cheshaowei 
	 * 
	 */
	@Override
	public boolean sendPush(String aliasId, String msgContent, RoleType roleType) {

		PushPayload pushPayload = PushPayload.newBuilder().setPlatform(Platform.android())
				.setAudience(Audience.alias(aliasId)).setMessage(Message.newBuilder().setMsgContent(msgContent).build())
				.build();

		return excuteSendPush(pushPayload, roleType);
	}

	/**
	 * 执行消息推送
	 */
	@Override
	public boolean excuteSendPush(PushPayload pushPayload, RoleType roleType) {

		try {

			PushResult pushResult = null;
			if (RoleType.Driver == roleType) {
				pushResult = super.driverJpushClient.sendPush(pushPayload);
			} else {
				pushResult = super.androidJPushClient.sendPush(pushPayload);
			}

		} catch (APIConnectionException e) {
			log.error("极光发送消息出现异常！", e);
			return true;
		} catch (APIRequestException e) {
			log.info("HTTP Status: " + e.getStatus());
			log.info("Error Code: " + e.getErrorCode());
			log.info("Error Message Id: " + e.getMsgId());
			log.info("Error Message: " + e.getErrorMessage());
			return true;
		}
		return true;
	}

}
